﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace PurchaseSales.Manage.Common
{
    public class StateManager : DependencyObject
    {
        public static string GetCurrentStateProperty(DependencyObject obj)
        {
            return (string)obj.GetValue(CurrentStatePropertyProperty);
        }

        public static void SetCurrentStateProperty(DependencyObject obj, string value)
        {
            obj.SetValue(CurrentStatePropertyProperty, value);
        }

        // Using a DependencyProperty as the backing store for CurrentStateProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentStatePropertyProperty =
            DependencyProperty.RegisterAttached("CurrentStateProperty", typeof(string),
            typeof(StateManager), new UIPropertyMetadata((s, e) =>
                                       {
                                           var propertyName = e.NewValue.ToString();
                                           var control = s as Control;
                                           if (control == null)
                                               throw new InvalidOperationException("This attached property only supports types derived from control");
                                           VisualStateManager.GoToState(control, propertyName, true);
                                       }));
    }
}
